package com.frxuexi.mdm2206

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.mdm2206.demo.Demo03
import com.frxuexi.mdm2206.demo.Demo04
import com.frxuexi.mdm2206.demo.Demo05
import com.frxuexi.mdm2206.demo.MyCom
import com.frxuexi.mdm2206.demo.MyCom2
import com.frxuexi.mdm2206.demo.MyPage1
import com.frxuexi.mdm2206.demo.MyPage2
import com.frxuexi.mdm2206.ui.theme.MDM2206Theme
import com.frxuexi.mdm2206.view.MainPage
import com.frxuexi.mdm2206.view.WelcomePage
import com.frxuexi.mymvi.MyAppsPage
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MDM2206Theme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
//                    1.创建 NavController
                    val navController = rememberNavController()
                    var startPage=RoutePath.WELCOME
                    val kv = MMKV.defaultMMKV()
                    //取值
                    val isIn = kv.decodeBool("isIn", false)
                    if(isIn){
                        startPage=RoutePath.MAIN
                    }/*else{
                        lifecycleScope.launch {
                            val async = async { delay(3000) }
                            async.await()
                            //存值
//                            kv.encode("isIn",true)
                            navController.navigate(RoutePath.MAIN)
                        }
                    }*/

                    NavHost(navController = navController, startDestination = startPage){
                        composable(RoutePath.WELCOME){ WelcomePage(navController) }
                        composable("page2/{myname}"){
                            val name = it.arguments?.getString("myname", "").toString()
                            MyPage2(name) }
                        composable(RoutePath.MAIN){ MainPage()}
                    }

                   /* AndroidView(factory = {
                        val webView = WebView(it)
                        webView.loadUrl("https://baijiahao.baidu.com/s?id=1816163679542548280&wfr=spider&for=pc")
//                        webView.addJavascriptInterface()
                        webView
                    })*/

                    /*lifecycleScope.launch {
                        listOf<Int>(4,5,6).asFlow().buffer().filter {
                           it%2==0
                        }.collect{
                            Log.i("===",it.toString())
                        }
                        flow<String> {
                            delay(3000)
                            emit("ok") }.collect{
                            Log.i("===",it.toString())
                        }
                    }
*/
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}



//@Preview:预览
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MDM2206Theme {
//        Greeting("Android")
        MyCom("World", 20)
    }
}